www.gusucode.com > C++ 实现的共生矩阵-源码程序 > C++ 实现的共生矩阵-源码程序/code/matrixnew.cpp
//Download by http://www.NewXing.com //matrix.cpp #include <iostream.h> //#include <alloc.h> #include <fstream> #include <cstdlib> #include <cmath> #include <vector> #include "d_matrix.h" template <typename T> void outputmat(const matrix<T>& mat); template <typename T> int classifymat(const matrix<T>& mat); template <typename T> void transformmat(const matrix<T>& formermat, matrix<T>& lattermat,int a,int b); template <typename T> void probablitymat(const matrix<T>& mat,matrix<T>& probmat); template <typename T> double as(const matrix<T>& mat,const matrix<T>& probmat); using namespace std; template <typename T> double con(const matrix<T>& mat,const matrix<T>& probmat); template <typename T> double cor(const matrix<T>& mat,const matrix<T>& probmat); template <typename T> double ent(const matrix<T>& mat,const matrix<T>& probmat); //主函数 int main() { //input matrix matrix<float> initMat; int numRows, numCols; //numrows为行,numcols为列 int i, j,a,b; double douasm=(double *)malloc(3*sizeof(double)); double doucor=(double *)malloc(3*sizeof(double)); double douent=(double *)malloc(3*sizeof(double)); double doucon=(double *)malloc(3*sizeof(double)); ifstream fin("mat.txt"); if(!fin) { cerr << "Cannot open 'mat.txt'" << endl; exit(1); } fin >> numRows >> numCols; initMat.resize(numRows, numCols); for(i = 0; i < numRows; i++) { for(j = 0; j < numCols; j++) { fin >> initMat[i][j]; } }//把文件中的矩阵读到相应数组中,读出几阶矩阵, for(i = 0; i < numRows; i++) { for(j = 0; j < numCols; j++) { cout << initMat[i][j]<< " "; } cout << endl; }//输出原始矩阵 cout << endl; //transform matrix to tempMat int counter=classifymat(initMat)+1; matrix<float> tempMat; tempMat.resize(counter, counter); for(i=0;i<4;i++) { switch(i) { case 0 : a=1;b=0;cout<<"水平方向相邻d=(1,0)"<<endl;break;//即水平方向相邻 case 1 : a=0;b=1;cout<<"竖直方向相邻d=(0,1)"<<endl;break;//即竖直方向相邻 case 2 : a=1;b=1;cout<<"西北一东南方向相邻(1,1)"<<endl;break;//即西北一东南方向相邻 case 3 : a=1;b=-1;cout<<"东北一西南方向相邻(1,-1)"<<endl;break;//即东北一西南方向相邻 } transformmat(initMat, tempMat,a,b); outputmat(tempMat); //输出共生矩阵 cout<<endl; //transform matrix to probMat matrix<float> probMat; probMat.resize(counter, counter); probablitymat(tempMat, probMat); // outputmat(probMat); cout << endl; //output the typicalarguements // cout<<endl; douasm[i]=as(tempMat, probMat); cout<<" 能量ASM为:"<<douasm[i]<<endl; doucon[i]=con(tempMat, probMat); cout<<" 对比度CON为:"<<doucon[i]<<endl; doucor[i]=cor(tempMat, probMat); cout<<" 相关性COR为:"<<doucor[i]<<endl; douent[i]=ent(tempMat, probMat); cout<<" 熵ENT为:"<<douent[i]<<endl; cout<<endl; } cout<<" 能量ASM平均值为:"<<(douasm[0]+douasm[1]+douasm[2]+douasm[3])/3.0<<endl; cout<<" 对比度CON平均值为:"<<(doucon[0]+doucon[1]+doucon[2]+doucon[3])/3.0<<endl; cout<<" 相关性COR平均值为:"<<(doucor[0]+doucor[1]+doucor[2]+doucor[3])/3.0<<endl; cout<<" 熵ENT平均值为:"<<(douent[0]+douent[1]+douent[2]+douent[3])/3.0<<endl; return 0; } //主函数结束 //outputmat matrix functrion输出函数 template <typename T> void outputmat(const matrix<T>& mat) { int i, j; for(i = 0; i < mat.rows(); i++) { for(j = 0; j < mat.cols(); j++) { cout << mat[i][j]<< " "; } cout << endl; } } //classifymat matrix function确定灰度级 template <typename T> int classifymat(const matrix<T>& mat) { vector<T> memoryval; memoryval.push_back(mat[0][0]); int counter=0; bool flag = false; int i, j; for(i = 0; i < mat.rows(); i++) { for(j = 0; j < mat.cols(); j++) { if(mat[i][j]>counter) counter=mat[i][j]; } } cout<<"灰度级为:"<<counter<<endl; return counter; } //transformmat matrix function求共生矩阵摸版 template <typename T> void transformmat(const matrix<T>& formermat, matrix<T>& lattermat,int a,int b) { //cout << "Plesase enter a and b: "; //int a=1, b=0; int matval = 0; //cin >> a >> b; int i, j, m, n; for(i = 0; i < lattermat.rows(); i++) { for(j = 0; j < lattermat.cols(); j++) { for(m = 0; m < formermat.rows(); m++) { for(n = 0; n < formermat.cols(); n++) { if(formermat[m][n]==i) { if(((m+a) < formermat.rows()) && ((n+b) < formermat.cols())) { if(formermat[m+a][n+b] == j) { matval++; } } } } } lattermat[i][j]=matval; matval=0; } } } //probablitymat matrix function//求个分值出现概率 template <typename T> void probablitymat(const matrix<T>& mat,matrix<T>& probmat) { T sum = T(); int i, j; for(i = 0; i < mat.rows(); i++) { for(j = 0; j < mat.cols(); j++) { sum += mat[i][j]; } } cout<<" sum="<<sum; cout << endl; for(i = 0; i < mat.rows(); i++) { for(j = 0; j < mat.cols(); j++) { probmat[i][j] = mat[i][j]/sum; } // cout << endl; } } //typicalargument 能量ASM template <typename T> double as(const matrix<T>& mat,const matrix<T>& probmat) { T e = T(); double a; int i,j; for(i = 0; i < probmat.rows(); i++) { for(j = 0; j < probmat.cols(); j++) { e += probmat[i][j]*probmat[i][j];//能量e } } a=e; return a; } //对比度CON template <typename T> double con(const matrix<T>& mat,const matrix<T>& probmat) { T con = T(); for(int i = 0; i < probmat.rows(); i++) { for(int j = 0; j < probmat.cols(); j++) { con += (i-j)*(i-j)*probmat[i][j]; } } return con; } //typicalargument 相关性C template <typename T> double cor(const matrix<T>& mat,const matrix<T>& probmat) { T cor = T(),mean = T(), stdvar = T(), sum = T(), var = T(); //typicalargument sum for(int i = 0; i < mat.rows(); i++) { for(int j = 0; j < mat.cols(); j++) { sum += mat[i][j]; } } //typicalargument mean mean = sum/(mat.rows()*mat.cols()); //typicalargument var for(i = 0; i < mat.rows(); i++) { for(int j = 0; j < mat.cols(); j++) { var += (mat[i][j]-mean)*(mat[i][j]-mean); } } //typicalargument stdvar stdvar=sqrt(var); //typicalargument 相关性C for(i = 0; i < probmat.rows(); i++) { for(int j = 0; j < probmat.cols(); j++) { cor += (i - mean)*(j - mean)*probmat[i][j];//相关性C } } cor /= (stdvar*stdvar); return cor; } //typicalargument 熵ENT template <typename T> double ent(const matrix<T>& mat,const matrix<T>& probmat) { T ent = T(); for(int i = 0; i < probmat.rows(); i++) { for(int j = 0; j < probmat.cols(); j++) { if(probmat[i][j])//? ent += probmat[i][j]*log(probmat[i][j])/log(10.0); } } ent = -ent; //熵ENT return ent; }